package com.vijayan.profile.util;

import java.util.Calendar;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.vijayan.profile.model.Profile;

public class ProfileUtil {
	public static String getProfileCode(Profile profile) {
		String idCodeStart = "0000000000";
		String idCodeEnd = "KM";
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int date = cal.get(Calendar.DATE);
		boolean male = profile.getGender();
		StringBuilder profileCode = new StringBuilder();	
		if(male){
			profileCode.append("M");
		} else {
			profileCode.append("F");
		}
		long autoId = 0l;
		try {
			autoId = CounterLocalServiceUtil.increment(Profile.class.getName());
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String idCodeMiddle = autoId + StringPool.BLANK;
		int length = idCodeStart.length() - idCodeMiddle.length();
		idCodeStart = idCodeStart.substring(0, length);
		profileCode.append(idCodeStart);
		profileCode.append(idCodeMiddle);
		profileCode.append(idCodeEnd);
		String profileName = profile.getProfileName();
		if (Validator.isNotNull(profileName) && profileName.length() >= 2) {
			profileName = profileName.substring(0, 2).toUpperCase();
		}
		profileCode.append(year);
		profileCode.append(month);
		profileCode.append(date);
		profileCode.append(profileName);
		return profileCode.toString();
	}
}